package com.algorithm.ocean.interview;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author PF.Tian
 * @since 2022/3/25
 */
public class Len002 {

    /*
     现有 /tmp/access.log 日志，格式为time|url|code|platform，如下为示例日志内容
     ……
     2020-09-01 10:01:00|http://www.taobao.com/hello|200|iOS
     2020-09-01 10:01:00|http://www.taobao.com/hello|200|iOS
     2020-09-01 10:01:00|http://www.taobao.com/hello|200|iOS
     2020-09-01 10:01:02|http://www.taobao.com/hello|200|Android
     2020-09-01 10:01:03|http://www.taobao.com/hello|200|iOS
     ……

     用java代码 统计下iOS渠道下的QPS。 */


    public static void main(String[] args) {
        String fileName = "D:/info.log";    // 文件绝对路径
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            int iosNum = 0;
            String low_s = "";
            String timeTemp = "";
            String low = null;
            while ((low = reader.readLine()) != null) {
                String time = low.substring(low.indexOf("2020-"), (low.indexOf("2020-") + 19));
                if (low.endsWith("iOS")) {
                    String ss = time.substring(time.lastIndexOf(":") + 1);  // 秒的位置
                    if (low_s.equals(ss)) {
                        iosNum++;
                    } else {
                        timeTemp = time;
                        low_s = ss;
                        iosNum = 1;
                    }
                } else {
                    System.out.println(timeTemp + " 时间段内的QPS: " + iosNum);
                    iosNum = 0;
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }


}
